<template>
	
	<view class="contentBox">
		<view class="publishBut"><button @click="toPublish" type="primary" plain="true" >发表</button></view>
		<template style=" padding-bottom: 10rpx;" v-if="isShow">
		<block v-for="(item,index) in publishList" :key='index'>
		<view class="publishList" >
		<view class="publishListTitle" @tap="toTopic(index)">{{item.title}}</view>
		<view class="publishListBottom"><text>{{item.time}}</text><text @click="publishDelect(index)">删除</text></view>
		</view>
		</block>
		</template>
		<template v-else>
			<no-thing ></no-thing>
		</template>
		</view>
	</view>
</template>

<script>
	import noThing from"../../components/common/no-thing.vue"
	import loadMore from "../../components/common/load-more.vue"
	export default {
		components: {
			noThing,
			loadMore
		},
		data() {
			return {
				publishList:[],
				isHave:true,
				isShow:true,
				loadtext:"上拉加载更多",
				pagenum:1,
				};
		},
		//监听触底事件
		onReachBottom(){
			this.loadmore()
		},
		onReady(){
			this.getMyPublish();
		},
		methods:{
			//获取用户发表的帖子
			getMyPublish(){
				uni.request({
					url:this.confige.webUrl+"/essay/listEssayByUserId",
					header:{
						'X-Token':this.User.token
					},
					data:{
						limits:15,
						pages:1,
					},
					success: (res) => {
						// console.log(res)
						if(res.data.code==400){
							this.isShow = false;
							return;
						}
						this.publishList = res.data.result[0].list;
						// console.log(this.publishList);
						if(this.publishList.length>14){
							this.loadtext ="上拉加载更多";
						}
						else{
							this.loadtext ="没有更多了";
						}
						if(res.code==400){
							this.isHave = false
						}
					}	
				})
			},
			loadmore(){
			   if(this.loadtext!="上拉加载更多"){ return;}
			   this.loadtext="加载中...";	
			   this.pagenum++;
			   setTimeout(() => {
				uni.request({
					url:this.confige.webUrl+"/essay/listEssayByUserId",
					data:{
						limits:15,
						pages:this.pagenum
					},
					header:{
						'X-Token':this.User.token
					},
					success: (res) => {
						console.log(res)
						 if(res.data.code==200){
							 let list = res.data.result[0].list;
							 this.publishList.push(...list) ;
							this.loadtext="上拉加载更多"
						 }
						 else{
						 	this.loadtext="没有更多啦"
						     }
					 }	
					}	  
				)}, 1000);
			},
			//前往编辑
			toPublish(){
				if(this.User.userRight.alias==="普通用户"){
					uni.showToast({
						icon:"none",
						title:"您无权限发帖"
					});return;
				}
				uni.navigateTo({
					url:"toPublish/toPublish"
				})
			},
			toTopic(index){
				// console.log(index);
				// console.log(this.publishList);
				var id = this.publishList[index].id
				console.log(id)
				uni.navigateTo({
					url:"../indexTopic/topic?id="+id
				})
			},
			//删除帖子
			publishDelect(index){
//				console.log(index)
			uni.showModal({
				title:"提示",
				content:'您确定要删除这条帖子吗',
				success: (res) => {
//					console.log(this.publishList[index].id)
					if (res.confirm) {
						uni.request({
							url:this.confige.webUrl+"/essay/delEssayByUserIdAndEssayId",
							header:{
								'X-Token':this.User.token
							},
							data:{
								essayId:this.publishList[index].id,
							},
							success: (res) => {
//								console.log(res)
								uni.showToast({
									title:"删除成功"
								})
								this.getMyPublish();
							}	
						    })
					        } else if (res.cancel) {
					            console.log('用户点击取消');
					        }
				}
			})
			}
		}
	}
</script>

<style lang="less" scoped>
page{ background-color: #FFFFFF;}
.publishBut{ width: 100%;height: 60upx;padding-top: 20upx; >button{ width:150upx;font-size: 30upx; height: 60upx;line-height: 60upx;float: right;}}
.contentBox{ width: 94%; margin: auto;}
.publishList{ border-bottom: 1upx solid #bfbfbf; margin-top:15upx;}
.publishListTitle{ overflow: hidden;white-space:nowrap;text-overflow: ellipsis;  font-size: 30upx;}
.publishListBottom{ display: flex;justify-content: space-between; padding: 15upx; font-size: 20upx;
>text:nth-of-type(1){
	  font-size: 25upx;
	   color:#777777;;
}
>text:nth-of-type(2){
	 border-left: 5upx solid #0081FF;
	  padding-left: 15upx;
	  font-size: 25upx;
}}

</style>
